﻿using DirectSell.IMongoDAL.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DirectSell.Model.Message;
using MongoDB.Driver;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.MongoDAL.User.Message
{
    /// <summary>
    /// 用户留言Mongo访问层
    /// </summary>
   public class MessageRepository : MongoBaseRepository,IMessageRepository
    {
        #region 构造函数
        public MessageRepository() : base("Messages")
        {
        }
        #endregion

        #region 标为已读
        /// <summary>
        /// 标为已读
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> AlRead(string id)
        {
            var model = await base.FindOneAsync<Messages>(id);
            model.State = 1;
            var result = await base.UpdateAsync<Messages>(model,id,true);
            return result.ModifiedCount > 0;
        }
        #endregion

        #region 查询我发出的留言
        /// <summary>
        /// 查询我发出的留言
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public async Task<PagedList<Messages>> AlreadyMessage(string usercode,int pageindex,int pagesize)
        {
            var where = Builders<Messages>.Filter.Eq("SendCode", usercode);
            var sort = Builders<Messages>.Sort.Ascending("State");
            return await base.FindListByPageAsync<Messages>(where, pageindex, pagesize, null, sort);
        }
        #endregion

        #region 删除一条留言
        /// <summary>
        /// 删除一条留言
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteMessage(string id)
        {
            var result = await base.DeleteAsync<Messages>(id);
            return result.DeletedCount > 0;
        }
        #endregion

        #region 写入一条留言
        /// <summary>
        /// 写入一条留言
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<bool> InsertMessage(MessageReq req)
        {
           var result = await base.AddAsync<MessageReq>(req);
           return result > 0;
        }
        #endregion

        #region 查发送给我的留言
        /// <summary>
        /// 查询发送给我的留言
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public async Task<PagedList<Messages>> MyMessage(string usercode,int pageindex,int pagesize)
        {
            var sort = Builders<Messages>.Sort.Ascending("State");
            var where = Builders<Messages>.Filter.Eq("ReciveCode", usercode);
            return await base.FindListByPageAsync<Messages>(where, pageindex, pagesize, null, sort);
            //return await base.FindListAsync<Messages>(where, null, sort);
        }
        #endregion

        #region 查一条留言
        /// <summary>
        /// 查一条留言
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Messages> QueryMsg(string id)
        {
            return await base.FindOneAsync<Messages>(id);
        }
        #endregion

        #region 查是否有未读留言
        /// <summary>
        /// 查询是否有唯独留言
        /// </summary>
        /// <returns></returns>
        public async Task<bool> QueryExist()
        {
            var filter = new List<FilterDefinition<Messages>>();
            filter.Add(Builders<Messages>.Filter.Eq("State", 0));
            filter.Add(Builders<Messages>.Filter.Eq("ReciveCode","admin"));
            return await base.CountAsync(Builders<Messages>.Filter.And(filter))>0;
        }
        #endregion
    }
}
